﻿using DAMS.Models;
using DAMS.Service;
using DAMS.Web.Areas.Manage.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace DAMS.Web.Areas.Manage.Controllers
{
    [Area("Manage")]
    public class ArticleController : Controller
    {
        private readonly IRepository _repo;
        private readonly IHostingEnvironment _env;
        private readonly SiteConfig _config;

        public ArticleController(IRepository repository, IHostingEnvironment env, IOptions<SiteConfig> option)
        {
            this._repo = repository;
            this._env = env;
            this._config = option.Value;
        }
        public IActionResult Index()
        {
            return View();
        }
        public IActionResult Create()
        {
            ArticleCreateViewModel acvm = new ArticleCreateViewModel();
            acvm.TreeJson= _repo.CategoryGetTreeJson();
            acvm.Config = _config;
            acvm.PublishTime = DateTime.Now;
            return View(acvm);
        }
        [HttpPost]
        public IActionResult Create([FromForm]ArticleCreateViewModel acvm)
        {
            Article entity = new Article();
            entity.Auth = acvm.Auth;
            entity.CarouselPath = acvm.CarouselPath;
            entity.CategoryID = acvm.CategoryID;
            entity.Content = acvm.Content;
            entity.CoverPath = acvm.CoverPath;
            entity.Excerpt = acvm.Excerpt;
            entity.isCarousel = !string.IsNullOrEmpty(entity.CarouselPath);
            entity.isPublish = acvm.isPublish;
            entity.isTop = acvm.isTop;
            entity.Title = acvm.Title;
            entity.PublishTime = acvm.PublishTime;

            if(Request.Form.ContainsKey("tagIdList"))
            {
                string tmpStrTagList = Request.Form["tagIdList"];
                string[] tmpList= tmpStrTagList.Split(",",StringSplitOptions.RemoveEmptyEntries);
                entity.ArticleTags = new System.Collections.Generic.List<ArticleTag>();
                foreach(string item in tmpList)
                {
                    ArticleTag at = new ArticleTag();
                    at.TagID= int.Parse(item);
                    at.Article = entity;
                    entity.ArticleTags.Add(at);
                }
            }
            Result<Article> result= _repo.ArticleCreate(entity);
            if(result.Succeeded)
            {
                return RedirectToRoute(new { Action = "List" });
            }
            else
            {
                return View();
            }
            
        }

        public IActionResult Add()
        {
            return View();
        }

        public IActionResult Details([FromRoute]int id)
        {
            Article model=  _repo.ArticleGetByID(id);
            return View(model);
        }

        public IActionResult List([FromQuery]int pageIndex=1,[FromQuery]int pageSize=25)
        {
            ParameterExpression exps = Expression.Parameter(typeof(Article), "a");
            Expression left = Expression.Property(exps, "ID");
            Expression right = Expression.Constant(6);
            Expression exp2 = Expression.LessThan(left, right);
            Expression<Func<Article, bool>> lamband1 = Expression.Lambda<Func<Article, bool>>(exp2, exps);
            //Dictionary<string, string> keys = new Dictionary<string, string> { { "ID", "id" }, { "Name", "name" }, { "Type", "type" }, { "ParentID", "pid" }, { "Children", "children" } };
            //ViewData["TreeJson"] = _repo.CategoryGetTreeJson(keys);
            ViewData["TreeJson"] = _repo.CategoryCutGetJson();
            return View(_repo.ArticleGetByPager(pageIndex, pageSize));
        }
    }
}